package com.zcgame.customview;

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import com.zcgame.base.utils.printDebug
import com.zcgame.customview.databinding.ListStatusView

/**
 * @author:huangLJ
 * @date:2021/5/6 19:04
 */
class LoadStateView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
    private val animator = AnimatorSet()

    private lateinit var view:View

    private var binding: ListStatusView? = DataBindingUtil.inflate<ListStatusView>(
            LayoutInflater.from(context),
            R.layout.widget_list_status_view,
            this, true
    )

    init {
        initAnimator()
    }


    private fun initAnimator() {
        val anim1 = ObjectAnimator.ofFloat(binding!!.widgetListStatusFrLoadingImg1, "scaleX", 0.8f, 1f, 0.8f).apply {
            //  设置无限重复
            repeatCount = -1
            interpolator = LinearInterpolator()
            //  反复交替
            repeatMode = ValueAnimator.REVERSE
            duration = 2000
        }
        val anim2 = ObjectAnimator.ofFloat(binding!!.widgetListStatusFrLoadingImg1, "scaleY", 0.8f, 1f, 0.8f).apply {
            //  设置无限重复
            repeatCount = -1
            interpolator = LinearInterpolator()
            //  反复交替
            repeatMode = ValueAnimator.REVERSE
            duration = 2000
        }
        val anim3 = ObjectAnimator.ofFloat(binding!!.widgetListStatusFrLoadingImg2, "scaleX", 0.8f, 1f, 0.8f).apply {
            //  设置无限重复
            repeatCount = -1
            interpolator = LinearInterpolator()
            //  反复交替
            repeatMode = ValueAnimator.REVERSE
            duration = 2000
        }
        val anim4 = ObjectAnimator.ofFloat(binding!!.widgetListStatusFrLoadingImg2, "scaleY", 0.8f, 1f, 0.8f).apply {
            //  设置无限重复
            repeatCount = -1
            interpolator = LinearInterpolator()
            //  反复交替
            repeatMode = ValueAnimator.REVERSE
            duration = 2000
        }

        val anim5 = ObjectAnimator.ofFloat(binding!!.widgetListStatusFrLoadingImg1,"rotation",0f,360f).apply {
            repeatCount = -1
            interpolator = LinearInterpolator()
            duration = 2000
        }
        val anim6 = ObjectAnimator.ofFloat(binding!!.widgetListStatusFrLoadingImg2,"rotation",0f,360f).apply {
            repeatCount = -1
            interpolator = LinearInterpolator()
            duration = 2000
        }
        animator.playTogether(anim1,anim2,anim3,anim4,anim5,anim6)
        animator.duration = 2000
        animator.interpolator = LinearInterpolator()
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        animator.start()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        animator.cancel()
    }

    fun setLoadView(){
        binding!!.widgetListStatusFrLoading.visibility = View.VISIBLE
        if (::view.isInitialized) {
            view.visibility = View.GONE
        }else{
            binding!!.listViewSub.viewStub!!.visibility = View.GONE
        }
    }

    fun setEmptyView(){
        binding!!.widgetListStatusFrLoading.visibility = View.GONE
        if (!binding!!.listViewSub.isInflated) {
            view = binding!!.listViewSub.viewStub!!.inflate()
        }else{
            view.visibility = View.VISIBLE
        }
    }
    fun setSuccess(){
        binding!!.widgetListStatusFrLoading.visibility = View.GONE
        if(::view.isInitialized ) view.visibility = View.GONE

    }

    fun setReloadListener(listener:OnClickListener) {
        binding!!.listViewSub.setOnInflateListener { stub, inflated ->
            inflated.findViewById<View>(R.id.widget_list_status_fr_empty).setOnClickListener(listener)
        }
    }
}
